#! /usr/bin/python
# -*- coding:utf-8 -*-

## ========================================================================
## Copyright (C) 2007-2013  Jean-Philippe GOLAY
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
## ========================================================================

from persistance import *


class Nav:
  
  def __init__(self):
    self.mDB=ManageDB(Glob.dbName, Glob.user, Glob.passwd, Glob.host)
    logging.basicConfig(filename=Glob.LOG_FILENAME,level=logging.DEBUG)

  ## id=row[0] name=row[1] title=row[2] parent_id=row[3]
  ## position=row[4] onclick=row[5] selected=row[6] page_id=row[7]
  def getMenu(self):
    if (self.mDB.executSQL(Glob.select_all_from_nav) == 0):
      logging.debug(u'Nav getMenu SQL error : %s' % Glob.select_all_from_nav)
    else:
      result=self.mDB.resultSQL()
      html_menu=""
      for row in result:
        if (row[6] == 0):
          if (row[3] == 0):
            html_menu += "<li>\n<a href='#' onclick='%s' title='%s' id='%s'>%s</a>\n</li>\n" % (row[5], row[2], row[7], row[1])
          else:
            html_menu += "<li>\n<a href='#' onclick='%s' title='%s' id='%s'>%s</a>\n" % (row[5], row[2], row[7], row[1])
            html_menu += self.getParentMenu(row[0])
            html_menu += "</li>\n"            
        else:
          self.default_page = row[7]
          if (row[3] == 0):
            html_menu += "<li>\n<a class='selected' href='#' onclick='%s' title='%s' id='%s'>%s</a>\n</li>\n" % (row[5], row[2], row[7], row[1])
          else:
            html_menu += "<li>\n<a class='selected' href='#' onclick='%s' title='%s' id='%s'>%s</a>\n" % (row[5], row[2], row[7], row[1])
            html_menu += self.getParentMenu(row[0])
            html_menu += "</li>\n"
            
      return html_menu

  def getParentMenu(self, id):
    if (self.mDB.executSQL(Glob.select_all_from_nav_where_parent % id) == 0):
      sql=Glob.select_all_from_nav_where_parent % id
      logging.debug(u'Nav getParentMenu Database error : %s' % sql)
      return ""
    else:
      result=self.mDB.resultSQL()
      html_parent=""
      if len(result) > 0:
        html_parent += "<ul>\n"
      
        for row in result:
          html_parent += "<li>\n<a href='#' onclick='%s' title='%s' id='%s'>%s</a>\n</li>\n" % (row[5], row[2], row[7], row[1])
        
        html_parent += "</ul>\n"      
        return html_parent
      else:
        return html_parent


  def getDefaultPageID(self):
    if self.default_page:
      return self.default_page
    else:
      return -1


  def setOneElement(self, id, name, title, parent_id, position, onclick, selected, page_id):
    u_name=unicode(name.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_title=unicode(title.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    if (self.mDB.executSQL(Glob.update_nav_where_id % (u_name, u_title, parent_id, position, onclick, selected, page_id, id) ) == 0):
      sql=Glob.update_nav_where_id % (u_name, u_title, parent_id, position, onclick, selected, page_id, id)
      logging.debug(u'Nav setOneElement SQL error : %s' % sql)
      return 0
    else:
      return 1


  def newElement(self, name, title, parent_id, position, onclick, selected, page_id):
    u_name=unicode(name.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_title=unicode(title.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    if (self.mDB.executSQL(Glob.insert_nav % (u_name, u_title, parent_id, position, onclick, selected, page_id) ) == 0):
      sql=Glob.insert_nav % (u_name, u_title, parent_id, position, onclick, selected, page_id)
      logging.debug(u'Nav newElement SQL error : %s' % sql)
      return 0
    else:
      if (self.mDB.executSQL(Glob.last_insert_id_nav ) == 0):
        logging.debug(u'Nav newElement SQL error : %s' % Glob.last_insert_id_nav)
        return 0
      else:
        result=self.mDB.resultSQL()
        for row in result:
          id=row[0]
        if id:
         return id 


  def deleteElement(self, page_id):
    if (self.mDB.executSQL(Glob.delete_nav_where_pageid % page_id) == 0):
      sql=Glob.delete_nav_where_pageid % page_id
      logging.debug(u'Nav deleteElement SQL error : %s' % sql)
      return 0
    else:
      return 1

      
  def getOneElement(self, id):
    if (self.mDB.executSQL(Glob.select_all_from_nav_where_id % id) == 0):
      sql=Glob.select_all_from_nav_where_id % id
      logging.debug(u'Nav getOneElement SQL error : %s' % sql)
    else:
      result=self.mDB.resultSQL()
      if len(result) == 0:
        return 0
      else:
        for row in result:
          self.nav_id=row[0]
          self.name=row[1]
          self.title=row[2]
          self.parent_id=row[3]
          self.position=row[4]
          self.onclick=row[5]
          self.selected=row[6]
          self.page_id=row[7]
        return { u'nav_id': self.nav_id, u'name': self.name, u'title': self.title, u'parent_id': self.parent_id, u'position': self.position, u'onclick': self.onclick, u'selected': self.selected, u'page_id': self.page_id }

